/*
 * IM - Image I/O Library
 * Copyright (c) 1996 CENPES/Petrobras, TeCGraf/PUC-Rio.
 * Antonio E. Scuri - scuri@tecgraf.puc-rio.br
 * 
 * BINUTIL.C
 * 
 * Contains the implementation of utility functions to handle binary data in diferent byte orders.
 *
 * History:
 *          1996.12.13 - Finished inicial version.
 *
 */

#include "binutil.h"



int binCPUByteOrder(void)
{
	unsigned short w = 0x0001;
	unsigned char* b = (unsigned char*)&w;
	return b[0] == 0x01? 0: 1;
}/* binCPUByteOrder */


void binInvertOrder2BytesBuf(unsigned short *pValues, unsigned long pSize)
{
	unsigned char* rPtrValue;
	unsigned char lTemp;

	while (pSize-- != 0)
	{
		rPtrValue = (unsigned char*)pValues;
		
		lTemp = rPtrValue[1];
		rPtrValue[1] = rPtrValue[0];
		rPtrValue[0] = lTemp;

		pValues++;
	}
}/* binInvertOrder2BytesBuf */


void binInvertOrder4BytesBuf(unsigned long *pValues, unsigned long pSize)
{
	unsigned char* rPtrValue;
	unsigned char lTemp;

	while (pSize-- != 0)
	{
		rPtrValue = (unsigned char*)pValues;

		lTemp = rPtrValue[3];
		rPtrValue[3] = rPtrValue[0];
		rPtrValue[0] = lTemp;

		lTemp = rPtrValue[2];
		rPtrValue[2] = rPtrValue[1];
		rPtrValue[1] = lTemp;

		pValues++;
	}
}/* binInvertOrder4BytesBuf */


void binInvertOrder8BytesBuf(double *pValues, unsigned long pSize)
{
	unsigned char* rPtrValue;
	unsigned char lTemp;

	while (pSize-- != 0)
	{
		rPtrValue = (unsigned char*)pValues;

		lTemp = rPtrValue[7];
		rPtrValue[7] = rPtrValue[0];
		rPtrValue[0] = lTemp;

		lTemp = rPtrValue[6];
		rPtrValue[6] = rPtrValue[1];
		rPtrValue[1] = lTemp;

		lTemp = rPtrValue[5];
		rPtrValue[5] = rPtrValue[2];
		rPtrValue[2] = lTemp;

		lTemp = rPtrValue[4];
		rPtrValue[4] = rPtrValue[3];
		rPtrValue[3] = lTemp;

		pValues++;
	}
}/* binInvertOrder8BytesBuf */



/* BINUTIL.C */
